/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

package jchip.tests;
import java.net.MalformedURLException;
import jchip.backend.replies.*;
import jchip.backend.*;
import java.io.*;

public class JChipConsole {
	public static void main(String[] args) throws IOException {
		INFLobbyClient client=null;
		
		try {
			client = new INFLobbyClient("http://www.cleeus.de/iw2p/xmlrpc.php");
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		System.out.println("> getTimings");
		INFLobbyPollTimings timings = client.getPollTimings();
		System.out.println(timings.waiting + " " + timings.playing);
	
		System.out.println("> getChallenge");
		INFLobbyChallenge challenge = client.getChallenge();
		
		System.out.println(challenge.challengeId + ", " + challenge.mimeType);
		System.out.println("captcha.jpeg");
		
		FileOutputStream out = new FileOutputStream("captcha.jpeg");
		out.write(challenge.challengeData);
		out.close();
		
		System.out.println("> getSessionId(" + challenge.challengeId + ", \"ABCDE\")");
		int sessionId = client.getSessionId(challenge.challengeId, "ABCDE");
		System.out.println(sessionId);
		
		System.out.println("> setNickname(" + sessionId + ", \"INF_Player\")");
		boolean success = client.setNickname(sessionId, "INF_Player");
		System.out.println(success);
		
		System.out.println("> getQueues(" + sessionId + ")");
		INFLobbyQueueInfo queueInfos[] = client.getQueues(sessionId);
		for (int i = 0; i < queueInfos.length; i++) {
			System.out.println(i + ":");
			System.out.println("    queueid=" + queueInfos[i].queueid);
			System.out.println("    count=" + queueInfos[i].count);
			System.out.println("    title=" + queueInfos[i].title);
			System.out.println("    ip=" + queueInfos[i].ip);
			System.out.println("    port=" + queueInfos[i].port);
			System.out.println("    description=" + queueInfos[i].description);
			System.out.println(" -------");
		}
		
		System.out.println("> setQueues(" + sessionId + ", [2,3])");
		int[] queueIds = {2, 3};
		success = client.setQueues(sessionId, queueIds);
		System.out.println(success);
		
		System.out.println("> setStatus(" + sessionId + ", 2)");
		success = client.setStatus(sessionId, 2);
		System.out.println(success);
		success = client.setStatus(sessionId, 0);
		System.out.println(success);
		//TODO - mysql_affected_rows behaviour not nice
		
		
		System.out.println("> pollUpdates(" + sessionId + ")");
		INFLobbyPollInfo pollInfo = client.pollUpdates(sessionId);
		if(pollInfo.queues!=null) {
			System.out.println(" queues:");
			for (int i = 0; i < pollInfo.queues.length; i++) {
				System.out.println("    queueid=" + pollInfo.queues[i].queueid);
				System.out.print("    users=");
				for (int j = 0; j < pollInfo.queues[i].userids.length; j++) {
					System.out.print(pollInfo.queues[i].userids[j] + " ");
				}
				System.out.println();
				System.out.println(" ----");
			}
		}
		System.out.println("users:");
		for (int i = 0; i < pollInfo.users.length; i++) {
			System.out.println("    " + pollInfo.users[i].nick + " " + pollInfo.users[i].userid + " " + pollInfo.users[i].status);
		}
		
		System.out.println("> pollKeepAlive(" + sessionId + ")");
		while(true) {
			success = client.pollKeepAlive(sessionId);
			System.out.println(success);
			try {
				Thread.sleep(timings.waiting*750);
			} catch (InterruptedException e) {
			}
		}
		
	}

}
